<template>
	<view>
		<type-tabs :tabs="currencyList" v-model="tabIndex" @change="currencyChange" v-if="!$store.state.checkMode"></type-tabs>
		<view class="cu-form-group padding-lr margin-top">
			<view class="title" style="width: 150rpx;">{{ remarkTitle }}</view>
			<input type="text" v-model="remarks" disabled />
			<view class="text-red" @tap="goSetting">{{ remarkNav }}</view>
		</view>
		<view class="cu-form-group padding-lr margin-top-xs">
			<view class="title" style="width: 150rpx;">{{ addressTitle }}</view>
			<input type="text" v-model="address" :placeholder="addressInfo" placeholder-class="text-gray text-df" disabled />
			<view class="text-red" @tap="goSetting">{{ addressNav }}</view>
		</view>
		<view class="bg-white padding margin-top-xs">
			<view style="width: 150rpx;">提现金额</view>
			<view class="margin-bottom flex align-center solid-bottom">
				<view class="title padding-right-xs" style="font-size: 30px">￥</view>
				<input
					type="digit"
					style="font-size: 18px;width: 100%;"
					maxlength="11"
					placeholder-class="text-gray"
					v-model="withdrawal"
					:placeholder="withdrawalInfo"
					@input="checkBalance"
				/>
			</view>
			<view class="flex justify-between">
				<view>
					<text style="width: 150rpx;">可提现：</text>
					<text class="text-pink padding-right-xs">{{ balanceIcon }}</text>
					<text class="text-pink">{{ balance }}</text>
				</view>
				<view class="text-red text-underline" @tap="allBalance">全部提取</view>
			</view>
		</view>
		<userButton @submitEvent="widthdraw" :btnDisabled="btndisabled">提取</userButton>
		<!-- 提现信息确认对话框 -->
		<view class="cu-modal" :class="checkFlg ? 'show' : ''">
			<view class="cu-dialog">
				<view class="bg-white content text-bold text-pink padding-tb text-xl">提现信息确认</view>
				<view class="cu-form-group padding-lr margin-top">
					<view class="title" style="width: 150rpx;">{{ remarkTitle }}</view>
					<view>{{ remarks }}</view>
				</view>
				<view class="cu-form-group padding-lr">
					<view class="title" style="width: 150rpx;">{{ addressTitle }}</view>
					<view>{{ address }}</view>
				</view>
				<view class="cu-form-group padding-lr ">
					<view class="title" style="width: 150rpx;">提现金额</view>
					<view class="text-red">{{ withdrawalShow }}</view>
				</view>
				<view class="cu-form-group padding-lr margin-bottom">
					<view class="title" style="width: 150rpx;">提现密码</view>
					<input type="text" password v-model="payPassword" placeholder="请输入提现密码" placeholder-class="text-gray text-df" class="solid-bottom" />
				</view>
				<view class="cu-bar bg-white justify-around action">
					<button class="cu-btn round bg-gray padding-lr-xl" @tap="cancelWithdraw">修改</button>
					<button class="cu-btn round bg-pink padding-lr-xl" @tap="comfirmWithdraw">提现</button>
				</view>
			</view>
		</view>
		<loading :loadModal="loadModal"></loading>
	</view>
</template>

<script>
import typeTabs from '@/components/type-tabs.vue';
import userButton from '@/components/user-button.vue';
var _self; //this容器
export default {
	components: {
		typeTabs,
		userButton
	},
	data() {
		return {
			currencyList: [
				{
					id: '',
					name: '人民币',
					type: 'CNY',
					address: '',
					remarks: '',
					balance: '0.00'
				},
				{
					id: '',
					name: 'BTC',
					type: 'BTC',
					address: '',
					remarks: '',
					balance: '0.00000000'
				},
				{
					id: '',
					name: 'ETH',
					type: 'ETH',
					address: '',
					remarks: '',
					balance: '0.000000'
				}
			],
			tabIndex: 0, // 当前菜单下标
			settleList: [], //已结算余额
			rateCurrency: '', //用户设置的提现方式
			withdrawal: '', //提现的金额
			balance: '', //可用余额
			remarks: '', //备注，姓名 或者 虚拟币备注
			address: '', //提现地址，支付宝账户 或者 虚拟币地址
			payPassword: '',
			setPayPassword: false, //是否设置了提现密码
			checkFlg: false, //提现确认对话框
			loadModal: false
		};
	},
	onLoad() {
		_self = this;
	},
	onShow() {
		_self.settleAccounts();
		_self.getAddressList();
		_self.getPayPassword();
		setTimeout(() => {
			_self.currencyChange();
		}, 500);
	},
	computed: {
		btndisabled() {
			if (this.address && this.address.length > 0 && this.$util.isInputNumber(this.withdrawal)) {
				if (this.tabIndex == 0) {
					if (this.remarks && this.remarks.length > 0) {
						return false;
					}
				} else {
					return false;
				}
			}
			return true;
		},
		remarkTitle() {
			return this.tabIndex == 0 ? '姓名' : '备注';
		},
		addressInfo() {
			return this.tabIndex == 0 ? '未设置支付宝账户，请先设置' : '未设置收款地址，请先设置';
		},
		addressTitle() {
			return this.tabIndex == 0 ? '支付宝' : '收款地址';
		},
		remarkNav() {
			let tmp = this.currencyList[this.tabIndex].remarks;
			return tmp && tmp.length > 0 ? '修改' : '设置';
		},
		addressNav() {
			let tmp = this.currencyList[this.tabIndex].address;
			return tmp && tmp.length > 0 ? '修改' : '设置';
		},
		withdrawalInfo() {
			return this.tabIndex == 0 ? '最低提现金额 1 元' : this.tabIndex == 1 ? '最低提现金额 0.0000001 BTC' : '最低提现金额 0.00001 ETH';
		},
		balanceIcon() {
			return this.tabIndex == 0 ? '￥' : this.tabIndex == 1 ? 'B' : 'E';
		},
		withdrawalShow() {
			//格式化显示的金额
			let amount = '';
			if (this.tabIndex == 0) {
				amount = this.$util.FixedCeilCoin(this.withdrawal, 2);
			} else if (this.tabIndex == 1) {
				amount = this.$util.FixedCeilCoin(this.withdrawal, 8);
			} else {
				amount = this.$util.FixedCeilCoin(this.withdrawal, 6);
			}
			return amount;
		}
	},
	methods: {
		/* 返回 */
		back() {
			uni.navigateBack({ delta: 1 });
		},

		//提取
		widthdraw() {
			if (parseFloat(_self.withdrawal) <= parseFloat(_self.balance)) {
				if (_self.tabIndex == 0) {
					if (parseFloat(_self.withdrawal) >= 1) {
						_self.checkFlg = true;
					} else {
						_self.$util.msg('最低提现金额 1 元');
					}
				} else if (_self.tabIndex == 1) {
					if (parseFloat(_self.withdrawal) >= 0.0000001) {
						_self.checkFlg = true;
					} else {
						_self.$util.msg('最低提现金额 0.0000001 BTC');
					}
				} else {
					if (parseFloat(_self.withdrawal) >= 0.00001) {
						_self.checkFlg = true;
					} else {
						_self.$util.msg('最低提现金额 0.00001 ETH');
					}
				}
			} else {
				_self.$util.msg('可提现额不足');
			}
		},

		cancelWithdraw() {
			_self.checkFlg = false;
		},

		comfirmWithdraw() {
			if (_self.payPassword.length > 0) {
				let tmptype = _self.tabIndex == 0 ? 'alipay' : _self.currencyList[_self.tabIndex].type;
				const param = {
					currency: _self.currencyList[_self.tabIndex].type,
					amount: _self.withdrawal,
					type: tmptype,
					payPassword: _self.payPassword
				};
				_self.cancelWithdraw();
				_self.loadModal = true;
				_self.$http.httpRequest('userWithdrawal/apply', param, true).then(res => {
					_self.loadModal = false;
					if (res._tranStatus) {
						uni.redirectTo({
							url: 'result?type=' + _self.tabIndex
						});
					} else {
						_self.$util.msg(res._exceptionLocalizedMessage);
					}
				});
			} else {
				_self.$util.msg('请输入提现密码');
			}
		},

		//检查提现金额
		checkBalance: function(event) {
			let tmpValue = event.target.value;
			let len = tmpValue.length;
			let dindex = tmpValue.indexOf('.');
			let afterdot = _self.tabIndex == 0 ? 4 : _self.tabIndex == 1 ? 10 : 8;
			if (dindex > -1 && len >= dindex + afterdot) {
				let tmplen = dindex + afterdot - 1;
				setTimeout(() => {
					_self.withdrawal = tmpValue.slice(0, tmplen);
				}, 10);
			}
		},

		//全部提取
		allBalance() {
			_self.withdrawal = _self.balance;
		},

		//设置提现地址
		goSetting() {
			let tmpUrl = '../user/settingWithdraw?s=w&type=' + _self.tabIndex + '&cnt=' + encodeURIComponent(JSON.stringify(_self.currencyList[_self.tabIndex]));
			if (!_self.setPayPassword) {
				tmpUrl = '../user/settingpayPassword?t=2&cnt=' + encodeURIComponent(tmpUrl);
			}
			uni.navigateTo({
				url: tmpUrl
			});
		},

		//提现方式切换时
		currencyChange() {
			_self.remarks = _self.currencyList[_self.tabIndex].remarks;
			_self.address = _self.currencyList[_self.tabIndex].address;
			_self.balance = _self.currencyList[_self.tabIndex].balance;
			_self.withdrawal = '';
		},

		//结算帐户余额信息
		settleAccounts() {
			_self.$http.httpRequest('accounts/settleAccounts', null, true).then(res => {
				if (res._tranStatus) {
					let tmplist = res._data.list;
					if (tmplist) {
						tmplist.forEach(item => {
							let cindex = 0;
							if (item.currency == 'ETH') {
								cindex = 2;
							} else if (item.currency == 'BTC') {
								cindex = 1;
							}
							_self.$set(_self.currencyList[cindex], 'balance', item.balance);
						});
					}
				}
			});
		},

		//提现地址详情
		getAddressList() {
			_self.$http.httpRequest('userWithdrawalAddress/userId', null, true).then(res => {
				if (res._tranStatus) {
					let tmplist = res._data.list;
					if (tmplist) {
						tmplist.forEach(item => {
							let cindex = 0;
							if (item.currency == 'ETH') {
								cindex = 2;
							} else if (item.currency == 'BTC') {
								cindex = 1;
							}
							let tmpremark = cindex == 0 ? item.name : item.remarks;
							_self.$set(_self.currencyList[cindex], 'id', item.id);
							_self.$set(_self.currencyList[cindex], 'address', item.address);
							_self.$set(_self.currencyList[cindex], 'remarks', tmpremark);
						});
					}
				}
			});
		},

		//是否设置提现密码
		getPayPassword() {
			_self.$http.httpRequest('user/isSetPayPassword', null, true).then(res => {
				if (res._tranStatus) {
					_self.setPayPassword = res._data.setPayPassword;
				}
			});
		}
	}
};
</script>

<style></style>
